<?php
require_once("aznusoap/libs/nusoap.php");
require_once("config.php");
require_once("functions.php");


//define function create user
$namespace = "AZCreateUser";

$server = new soap_server();
$server->configureWSDL($namespace);
$server->wsdl->schemaTargetNamespace = $namespace;

$server->register(  'CreateUser'
				  , array(  'id'          => 'xsd:int'
						  , 'name'         => 'xsd:string'
						  , 'check'   => 'xsd:string'
                          , 'mail'         => 'xsd:string'
						  , 'phone'         => 'xsd:string'
                          , 'parentid'        => 'xsd:int')

				  , array('return'=>'xsd:int')
                  , $namespace
                  , false
                  , 'rpc'
                  , 'encoded'
                  , 'Receiving AZ from SMS Create User');

function CreateUser($id, $name, $check, $mail, $phone, $parentid)
{
	global $conf;	
	if($id != '' && $name != '' && $check != '' && $phone != ''){
		//Luu du lieu
		try {
	       	$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
			mysql_select_db($conf['db_name']);
			$sqlInsert = "INSERT INTO `sms_user`(`user_id_reg`, `user_name`, `user_check`, `user_email`, `parent_id`, `mobile`) VALUES('".intval($id)."','".cleanValue($name)."','".cleanValue($check)."','".cleanValue($mail)."','".cleanValue($parentid)."','".cleanValue($phone)."')";
			mysql_query($sqlInsert);		
			mysql_close($con);
			
			//Tao Thanh Cong			
			return "1";		
		} catch (Exception $e) {
			//Khong thanh cong
			return "0";
		}
		
	}
	//Khong thanh cong
	return "0";
}

$server->register(  'GetUserStatus'
				  , array(  'id'          => 'xsd:int')
				  , array('return'=>'xsd:int')
                  , $namespace
                  , false
                  , 'rpc'
                  , 'encoded'
                  , 'Receiving AZ from SMS Get User Status');

function GetUserStatus($id)
{
	global $conf;	
	if($id != ''){
		//Luu du lieu
		try {
	       	$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
			mysql_select_db($conf['db_name']);
			$sqlselect = "Select * From `sms_user` Where `user_id_reg` = '".intval($id)."' limit 1";
			$req = mysql_query($sqlselect);	
			while ($arr = mysql_fetch_array($req)) {				
				if ($arr['status'] == 1) {
					//Da kich hoat sms
					return "1";	
				}else{
					//Chua kich hoat sms
					return "0";						
				}
			}
			mysql_close($con);
				
		} catch (Exception $e) {
			//Khong thanh cong
			return "0";
		}
		
	}
	//Khong thanh cong
	return "0";
}

$server->register(  'GetUserBank'
				  , array(  'id'          => 'xsd:int')
				  , array('return'=>'xsd:int')
                  , $namespace
                  , false
                  , 'rpc'
                  , 'encoded'
                  , 'Receiving AZ from SMS Get User Bank');

function GetUserBank($id)
{
	global $conf;	
	if($id != ''){
		//Luu du lieu
		try {
	       	$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
			mysql_select_db($conf['db_name']);
			$sqlselect = "Select * From `sms_user` Where `user_id_reg` = '".intval($id)."' limit 1";
			$req = mysql_query($sqlselect);	
			while ($arr = mysql_fetch_array($req)) {				
				if ($arr['bank'] == 1) {
					//Da kich hoat sms
					mysql_query("Update `sms_user` Set bank = '0' Where `user_id_reg` = '".intval($id)."'");
					return "1";	
					
				}else{
					//Chua kich hoat sms
					return "0";						
				}
			}
			mysql_close($con);
				
		} catch (Exception $e) {
			//Khong thanh cong
			return "0";
		}
		
	}
	//Khong thanh cong
	return "0";
}
          

$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';

$server->service($POST_DATA);

exit();
?>